home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / wsgiref / simple_server.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  5.9 KB  |  159 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''BaseHTTPServer that implements the Python WSGI protocol (PEP 333, rev 1.21)
  5.  
  6. This is both an example of how WSGI can be implemented, and a basis for running
  7. simple web applications on a local machine, such as might be done when testing
  8. or debugging an application.  It has not been reviewed for security issues,
  9. however, and we strongly recommend that you use a "real" web server for
  10. production use.
  11.  
  12. For example usage, see the \'if __name__=="__main__"\' block at the end of the
  13. module.  See also the BaseHTTPServer module docs for other API information.
  14. '''
  15. from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
  16. import urllib
  17. import sys
  18. from wsgiref.handlers import SimpleHandler
  19. __version__ = '0.1'
  20. __all__ = [
  21.     'WSGIServer',
  22.     'WSGIRequestHandler',
  23.     'demo_app',
  24.     'make_server']
  25. server_version = 'WSGIServer/' + __version__
  26. sys_version = 'Python/' + sys.version.split()[0]
  27. software_version = server_version + ' ' + sys_version
  28.  
  29. class ServerHandler(SimpleHandler):
  30.     server_software = software_version
  31.     
  32.     def close(self):
  33.         
  34.         try:
  35.             self.request_handler.log_request(self.status.split(' ', 1)[0], self.bytes_sent)
  36.         finally:
  37.             SimpleHandler.close(self)
  38.  
  39.  
  40.  
  41.  
  42. class WSGIServer(HTTPServer):
  43.     '''BaseHTTPServer that implements the Python WSGI protocol'''
  44.     application = None
  45.     
  46.     def server_bind(self):
  47.         '''Override server_bind to store the server name.'''
  48.         HTTPServer.server_bind(self)
  49.         self.setup_environ()
  50.  
  51.     
  52.     def setup_environ(self):
  53.         env = self.base_environ = { }
  54.         env['SERVER_NAME'] = self.server_name
  55.         env['GATEWAY_INTERFACE'] = 'CGI/1.1'
  56.         env['SERVER_PORT'] = str(self.server_port)
  57.         env['REMOTE_HOST'] = ''
  58.         env['CONTENT_LENGTH'] = ''
  59.         env['SCRIPT_NAME'] = ''
  60.  
  61.     
  62.     def get_app(self):
  63.         return self.application
  64.  
  65.     
  66.     def set_app(self, application):
  67.         self.application = application
  68.  
  69.  
  70.  
  71. class WSGIRequestHandler(BaseHTTPRequestHandler):
  72.     server_version = 'WSGIServer/' + __version__
  73.     
  74.     def get_environ(self):
  75.         env = self.server.base_environ.copy()
  76.         env['SERVER_PROTOCOL'] = self.request_version
  77.         env['REQUEST_METHOD'] = self.command
  78.         if '?' in self.path:
  79.             (path, query) = self.path.split('?', 1)
  80.         else:
  81.             path = self.path
  82.             query = ''
  83.         env['PATH_INFO'] = urllib.unquote(path)
  84.         env['QUERY_STRING'] = query
  85.         host = self.address_string()
  86.         if host != self.client_address[0]:
  87.             env['REMOTE_HOST'] = host
  88.         
  89.         env['REMOTE_ADDR'] = self.client_address[0]
  90.         if self.headers.typeheader is None:
  91.             env['CONTENT_TYPE'] = self.headers.type
  92.         else:
  93.             env['CONTENT_TYPE'] = self.headers.typeheader
  94.         length = self.headers.getheader('content-length')
  95.         if length:
  96.             env['CONTENT_LENGTH'] = length
  97.         
  98.         for h in self.headers.headers:
  99.             (k, v) = h.split(':', 1)
  100.             k = k.replace('-', '_').upper()
  101.             v = v.strip()
  102.             if k in env:
  103.                 continue
  104.             
  105.             if 'HTTP_' + k in env:
  106.                 env['HTTP_' + k] += ',' + v
  107.                 continue
  108.             env['HTTP_' + k] = v
  109.         
  110.         return env
  111.  
  112.     
  113.     def get_stderr(self):
  114.         return sys.stderr
  115.  
  116.     
  117.     def handle(self):
  118.         '''Handle a single HTTP request'''
  119.         self.raw_requestline = self.rfile.readline()
  120.         if not self.parse_request():
  121.             return None
  122.         handler = ServerHandler(self.rfile, self.wfile, self.get_stderr(), self.get_environ())
  123.         handler.request_handler = self
  124.         handler.run(self.server.get_app())
  125.  
  126.  
  127.  
  128. def demo_app(environ, start_response):
  129.     StringIO = StringIO
  130.     import StringIO
  131.     stdout = StringIO()
  132.     print >>stdout, 'Hello world!'
  133.     print >>stdout
  134.     h = environ.items()
  135.     h.sort()
  136.     for k, v in h:
  137.         print >>stdout, k, '=', repr(v)
  138.     
  139.     start_response('200 OK', [
  140.         ('Content-Type', 'text/plain')])
  141.     return [
  142.         stdout.getvalue()]
  143.  
  144.  
  145. def make_server(host, port, app, server_class = WSGIServer, handler_class = WSGIRequestHandler):
  146.     '''Create a new WSGI server listening on `host` and `port` for `app`'''
  147.     server = server_class((host, port), handler_class)
  148.     server.set_app(app)
  149.     return server
  150.  
  151. if __name__ == '__main__':
  152.     httpd = make_server('', 8000, demo_app)
  153.     sa = httpd.socket.getsockname()
  154.     print 'Serving HTTP on', sa[0], 'port', sa[1], '...'
  155.     import webbrowser
  156.     webbrowser.open('http://localhost:8000/xyz?abc')
  157.     httpd.handle_request()
  158.  
  159.